package tagLibrary;

import psl.*;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.sql.*;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Feb 26, 2007
 * Time: 11:25:08 AM
 */
public class SearchTag extends TagSupport
{
    private String Query = null;
    private String OnClick = null;
    private String ROType = null;
    private String ROCondition = null;
    private BasePage ROPage = null;
    ResultSet mResultSet;
    UserSession ROSession;
    JspWriter out;

    public void setROPage (BasePage foBasePage)
    {
        this.ROPage = foBasePage;
    }

    public void setSQLQuery (String fsQuery)
    {
        this.Query = fsQuery;
    }

    public void setROCondition (String fsROCondition)
    {
        this.ROCondition = fsROCondition;
    }

    public void setROSession (UserSession fsSession)
    {
        this.ROSession = fsSession;
    }

    public void setONClick (String fsClick)
    {
        this.OnClick = fsClick;
    }

    public void setROType (String fsROType)
    {
        this.ROType = fsROType;
    }

    public int doStartTag () throws JspException
    {
        super.doStartTag ();
        out = pageContext.getOut ();
        try
        {
            if (ROType.equalsIgnoreCase ("City"))
            {
                mResultSet = DBConn.getRecordFromDB (Query);
                String lsText = ROHTMLServices.getCityList (mResultSet);
                out.write (lsText);
                mResultSet.close ();
            }
            if (ROType.equalsIgnoreCase ("UserAccount"))
            {
                if (ROPage.maRows != null)
                {

                }
                else
                {
                    String lsCondition = "cid='" + ROSession.msCustId + "'";
                    String[] lsColumn = { "order_no", "product_name", "company", "product_price", "quantity", "total", "order_date", "status", "product_id" };
                    ROPage.msCondition = (ROPage.msCondition != null && !ROPage.msCondition.equals (""))
                            ? ((ROCondition != null && !ROCondition.equals ("")) ? (lsCondition + ROCondition) : lsCondition)
                            : ((ROCondition != null && !ROCondition.equals ("")) ? (lsCondition + ROCondition) : lsCondition);
                    ROPage.maRows = DBConn.setRecordsToString ("select * from tempdata", lsColumn, ROPage.msCondition);
                    if (ROPage.maRows != null)
                    {
                        ROPage.miRows = ROPage.maRows[0].length;
                    }
                    ROPage.miEndRow = ((ROPage.miRows / 10) < 1) ? ROPage.miRows : 10;
                }
                String lsText = ROHTMLServices.getUserProductDeatil (ROPage);
                out.write (lsText);
            }
            if (ROType.equalsIgnoreCase ("ItemBill") || ROType.equalsIgnoreCase ("SaleEntryBill"))
            {
                if (ROSession.mvSelsected != null)
                {
                    if (ROSession.mvSelsected.size () == 0)
                    {
                        out.write ("<table border=1 cellpadding=0 cellspacing=0 width=50% >\r\n" +
                                "<th class='thwide'>INDEX</th ><th class='thwide' >NAME</th><th class='thwide'>COMAPY</th>\r\n" +
                                "<th class='thwide'>PRICE</th><th class='thwide'>QUANTITY</th ><th class='thwide'>TOTAL</th ><th class='thwide' align='center'>REMOVE</th>\r\n");
                        out.write ("<tr><td bgcolor=#faf2e1 height=30 colspan=7><h2>NO Item Selected</h2></td></tr>");
                        out.write ("</table>");
                    }
                    else
                    {
                        String lsPid = "";
                        String[] laSelected = CommonServices.ArrayListToString (ROSession.mvSelsected);
                        int liLength = laSelected.length;
                        for (int i = 0; i < liLength; i++)
                        {
                            if (i == liLength - 1)
                            {
                                lsPid += "'" + laSelected[i] + "'";
                            }
                            else
                            {
                                lsPid += "'" + laSelected[i] + "'" + ", ";
                            }
                        }
                        System.err.println("lsPid: "+lsPid);
                        String laValue[][]=null;
                        if (ROPage.maRows != null)
                        {
                        }
                        else
                        {
                            String[] laColumn = { "Product_Id", "Product_Name", "Cat_Name", "Company", "Product_Price", "Qty_On_Hand", "Re_ord_level", "img" };
                            laValue = DBConn.setRecordsToString (Query, laColumn, "Product_Id IN (" + lsPid + ")");
                            ROPage.maRows = laValue;
                        }
                        String lsText = ROHTMLServices.getItemBillList (ROSession, ROType, ROPage);
                        out.write (lsText);
                    }
                }
                else
                {

                }
            }

           if (ROType.equalsIgnoreCase ("CategoryList"))
            {
                mResultSet = DBConn.getRecordFromDB (Query);
                String lsText = ROHTMLServices.getCategoryList (mResultSet, OnClick);
                out.write (lsText);
                mResultSet.close ();
            }
            if (ROType.equalsIgnoreCase ("ItemCategoryList"))
            {
                mResultSet = DBConn.getRecordFromDB (Query);
                String lsText = ROHTMLServices.getCategoryListItem (mResultSet, OnClick);
                out.write (lsText);
                mResultSet.close ();
            }
            if (ROType.equalsIgnoreCase ("ItemList"))
            {
                if (ROSession.msViewPage.equalsIgnoreCase ("ItemList.jsp"))
                {
                    HashMap lvData=new HashMap ();
                    String[][] laValue=null;
                    String msCategory=null;
                    if (ROPage.maRows != null)
                    {
                        msCategory = ROSession.msPageID;
                    }
                    else
                    {
                        msCategory = ROSession.msPageID;
                        String[] laColumn = { "Product_Id", "Product_Name", "Cat_Name", "Company", "Product_Price", "Qty_On_Hand", "Re_ord_level", "img" };
                        laValue = DBConn.setRecordsToString (Query, laColumn, "");
                        ROPage.maRows = laValue;
                        if (ROPage.maRows != null)
                        {
                            ROPage.miRows = laValue.length;
                        }
                        ROPage.miEndRow = ((ROPage.miRows / 4) < 1) ? ROPage.miRows : 4;
                    }

                    String lsText = ROHTMLServices.getItemList (ROPage, msCategory);
                    out.write (lsText);
                }
            }
        }
        catch (Exception e)
        {
            System.out.println ("Exception Occures in SearchTag.java");
            e.printStackTrace ();
        }
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag () throws JspException
    {
        super.doEndTag ();
        try
        {
        }
        catch (Exception e)
        {
            System.out.println ("Exception " + e);
        }
        return EVAL_PAGE;
    }
}
